package com.bw.module_home.visit.viewmodel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bw.lib_common.base.BaseViewModel
import com.bw.module_home.visit.model.AddVisitEntity
import com.bw.module_home.visit.model.VisitEntity
import com.bw.module_home.visit.model.VisitRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import javax.inject.Inject

//class VisitViewModel: BaseViewModel2<VisitRepository>() {
//    override fun createRepository(): VisitRepository = VisitRepository()
//    val getVisitListSuccess by lazy { MutableLiveData<VisitEntity>() }
//    val addVisitSuccess by lazy { MutableLiveData<AddVisitEntity>() }
//    fun getVisitList() {
//        mViewModelScope.launch (mDispatcher){
//            val result = mRepository.getVisitList()
//            if (result.code == 200){
//                getVisitListSuccess.postValue(result.data)
//            }else{
//                mErrorLiveData.postValue(result.message)
//            }
//        }
//    }
//    fun addVisit(addVisitEntity: AddVisitEntity) {
//        mViewModelScope.launch (mDispatcher){
//            val result = mRepository.addVisit(addVisitEntity)
//            if (result.code == 200){
//                addVisitSuccess.postValue(result.data)
//            }else{
//                mErrorLiveData.postValue(result.message)
//            }
//        }
//    }
//}
@HiltViewModel
class VisitViewModel @Inject constructor(
    private val visitRepository: VisitRepository
) : BaseViewModel() {

    val getVisitListSuccess = MutableLiveData<List<VisitEntity>>()
    val addVisitSuccess = MutableLiveData<List<AddVisitEntity>>()

    fun getVisitList() {
        visitRepository.getVisitList()
            .onEach { result ->
                if (result.code == 200) {
                    result.data?.let {
                        // 更新 LiveData 的值
                        val currentList = getVisitListSuccess.value?.toMutableList() ?: mutableListOf()
                        currentList.add(it)
                        getVisitListSuccess.value = currentList
                    }
                } else {
                    Log.e("VisitViewModel", "获取拜访列表失败  ${result.message}")
                }
            }
            .catch { e ->
                Log.e("VisitViewModel", "获取拜访列表异常: ${e.message}")
            }
            .launchIn(viewModelScope)
    }

    fun addVisit(addVisitEntity: AddVisitEntity) {
        visitRepository.addVisit(addVisitEntity)
            .onEach { result ->
                Log.d("add_visit", "addVisit:${result.data}")
                if (result.code == 200) {
                    result.data?.let {
                        // 更新 LiveData 的值
                        val currentList = addVisitSuccess.value?.toMutableList() ?: mutableListOf()
                        currentList.add(it)
                        addVisitSuccess.value = currentList
                    }
                } else {
                    Log.e("VisitViewModel", "添加拜访失败 ${result.message}")
                }
            }
            .catch { e ->
                Log.e("VisitViewModel", "添加拜访异常: ${e.message}")
            }
            .launchIn(viewModelScope)
    }
}